package com.spring.demo.weixinpay.utils;

import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.helpers.XMLFilterImpl;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.servlet.http.HttpServletRequest;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.*;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.*;

/**
 * 微信公众平台开发 通用的工具类
 */
public class CommonUtil {

    private static Logger log=Logger.getLogger(CommonUtil.class);
    //private Log log = (Log) Logger.getLogger(this.getClass());
    /**
     *
     * @param requestUrl     接口地址
     * @param requestMethod  请求方法：POST、GET...
     * @param output         接口入参
     * @param needCert       是否需要数字证书
     * @return
     */
    public static StringBuffer httpsRequest(String requestUrl, String requestMethod, String output,boolean needCert)
            throws NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException, MalformedURLException,
            IOException, ProtocolException, UnsupportedEncodingException {


        URL url = new URL(requestUrl);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

        //是否需要数字证书
        if(needCert){
            //设置数字证书
            setCert(connection);
        }
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod(requestMethod);
        if (null != output) {
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(output.getBytes("UTF-8"));
            outputStream.close();
        }

        // 从输入流读取返回内容
        InputStream inputStream = connection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String str = null;
        StringBuffer buffer = new StringBuffer();
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }

        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        inputStream = null;
        connection.disconnect();
        return buffer;
    }

    /**
     * 给HttpsURLConnection设置数字证书
     * @param connection
     * @throws IOException
     */
    public static void setCert(HttpsURLConnection connection) throws IOException{
        FileInputStream instream = null;
        try {
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            //读取本机存放的PKCS12证书文件
            instream = new FileInputStream(new File("certPath")); //certPath:数字证书路径

            //指定PKCS12的密码(商户ID)
            keyStore.load(instream, "商户ID".toCharArray());
            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "商户ID".toCharArray()).build();
            //指定TLS版本
            SSLSocketFactory ssf = sslcontext.getSocketFactory();
            connection.setSSLSocketFactory(ssf);
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            instream.close();
        }
    }

    /**
     * 如果返回JSON数据包,转换为 JSONObject
     * @param requestUrl
     * @param requestMethod
     * @param outputStr
     * @param needCert
     * @return
     */
    public static JSONObject httpsRequestToJsonObject(String requestUrl, String requestMethod, String outputStr, boolean needCert) {
        JSONObject jsonObject = null;
        try {
            StringBuffer buffer = httpsRequest(requestUrl, requestMethod, outputStr,needCert);
            jsonObject = JSONObject.fromObject(buffer.toString());
        } catch (ConnectException ce) {
            log.error("连接超时："+ce.getMessage());
        } catch (Exception e) {
            log.error("https请求异常："+e.getMessage());
        }

        return jsonObject;
    }

    /**
     * 如果返回xml数据包，转换为Map<String, String>
     * @param requestUrl
     * @param requestMethod
     * @param outputStr
     * @param needCert
     * @return
     */
    public static Map<String, String> httpsRequestToXML(String requestUrl, String requestMethod, String outputStr, boolean needCert) {
        Map<String, String> result = new HashMap<>();
        try {
            StringBuffer buffer = httpsRequest(requestUrl, requestMethod, outputStr,needCert);
            result = parseXml(buffer.toString());
        } catch (ConnectException ce) {
            log.error("连接超时："+ce.getMessage());
        } catch (Exception e) {
            log.error("https请求异常："+e.getMessage());
        }
        return result;
    }

    /**
     * xml转为map
     * @param xml
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXml(String xml)  {
        Map<String, String> map = new HashMap<String, String>();
        try {
            Document document = DocumentHelper.parseText(xml);

            Element root = document.getRootElement();
            List<Element> elementList = root.elements();

            for (Element e : elementList){
                map.put(e.getName(), e.getText());
            }
        } catch (DocumentException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return map;
    }

    public static Map<Object, Object> parseXml(HttpServletRequest request)
    {
        // 解析结果存储在HashMap
        Map<Object, Object> map = new HashMap<Object, Object>();
        try {
            InputStream inputStream;

            inputStream = request.getInputStream();

            // 读取输入流
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputStream);
            System.out.println(document);
            // 得到xml根元素
            Element root = document.getRootElement();
            // 得到根元素的所有子节点
            List<Element> elementList = root.elements();

            // 遍历所有子节点
            for (Element e : elementList)
                map.put(e.getName(), e.getText());

            // 释放资源
            inputStream.close();
            inputStream = null;
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (DocumentException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return map;
    }

    /**
     * 将一个 JavaBean 对象转化为一个  Map
     * @param bean 要转化的JavaBean 对象
     * @return 转化出来的  Map 对象
     * @throws IntrospectionException 如果分析类属性失败
     * @throws IllegalAccessException 如果实例化 JavaBean 失败
     * @throws InvocationTargetException 如果调用属性的 setter 方法失败
     */
    @SuppressWarnings({ "rawtypes"})
    public static SortedMap<Object,Object> convertBean(Object bean) {
        SortedMap<Object,Object> returnMap = new TreeMap<Object,Object>();
        try {
            Class type = bean.getClass();
            BeanInfo beanInfo = Introspector.getBeanInfo(type);
            PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
            for (int i = 0; i< propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                if (!propertyName.equals("class")) {
                    Method readMethod = descriptor.getReadMethod();
                    Object result = readMethod.invoke(bean, new Object[0]);
                    if (result != null) {
                        returnMap.put(propertyName, result);
                    } else {
                        returnMap.put(propertyName, "");
                    }
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return returnMap;
    }


    /**
     * 生成签名
     * @param parameters
     * @return
     */
    public static String createSgin(SortedMap<Object,Object> parameters)
    {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        /** 商户支付app-key*/
        sb.append("key=" + "");
        String sgin= MD5.sign(sb.toString());
        return sgin;
    }

    /**
     * 获取ip地址
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            return request.getRemoteAddr();
        }
        byte[] ipAddr = addr.getAddress();
        String ipAddrStr = "";
        for (int i = 0; i < ipAddr.length; i++) {
            if (i > 0) {
                ipAddrStr += ".";
            }
            ipAddrStr += ipAddr[i] & 0xFF;
        }
        return ipAddrStr;
    }

}
